package com.example.mannortycoon.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.example.mannortycoon.entity.CropEntity;
import com.example.mannortycoon.service.CropService;
import com.example.mannortycoon.uitl.JWTUtils;
import com.example.mannortycoon.uitl.PageUtils;
import com.example.mannortycoon.uitl.R;
import com.example.mannortycoon.vo.CropVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("game/crop")
@Api(tags = "作物")
public class CropController {

    private final CropService cropService;

    @Autowired
    public CropController(CropService cropService){
        this.cropService = cropService;
    }

    /**
     * app获取图鉴
     */
    @GetMapping("/getCropList")
    @ApiOperation("图鉴列表")
    public R getCropList(HttpServletRequest request){
        Long userId = JWTUtils.getId(request.getHeader("token"));
        List<CropVO> cropList = cropService.getDetailList(userId);
        return R.ok().put("data",cropList);
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    @ApiIgnore
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = cropService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{cropId}")
    @ApiIgnore
    public R info(@PathVariable("cropId") Long cropId){
		CropEntity crop = cropService.getById(cropId);
        return R.ok().put("crop", crop);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @ApiIgnore
    public R save(@RequestBody CropEntity crop){
		cropService.save(crop);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @ApiIgnore
    public R update(@RequestBody CropEntity crop){
        crop.setUpdateTime(new Date());
		cropService.updateById(crop);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @ApiIgnore
    public R delete(@RequestBody Long[] cropIds){
		cropService.deleteBatch(Arrays.asList(cropIds));
        return R.ok();
    }

}
